package com.sh.shanhuo.adapter;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sh.shanhuo.R;
import com.sh.shanhuo.database.web.WebInfoEntity;
import com.sh.shanhuo.utils.Tools;

import java.util.List;

import androidx.appcompat.widget.AppCompatImageView;

public class ContentAdapter extends BaseQuickAdapter<WebInfoEntity, BaseViewHolder> {

    private final Activity activity;
    private OnMoreClickListener onMoreClickListener;
    private String pushPos;
    private String fixedPushId;

    public ContentAdapter(Activity activity, List<WebInfoEntity> webInfoEntities) {
        super(R.layout.website_view, webInfoEntities);
        this.activity = activity;
    }

    @Override
    protected void convert(BaseViewHolder helper, WebInfoEntity webInfoEntity) {
        int curPos = helper.getAdapterPosition();
        RelativeLayout parentLayout = helper.getView(R.id.parentLayout);
        ImageView title = helper.getView(R.id.title);
        ImageView offline = helper.getView(R.id.offline);
        AppCompatImageView logo = helper.getView(R.id.logo);
        ImageView more = helper.getView(R.id.more);
        TextView webTitle = helper.getView(R.id.webTitle);
        TextView webContent = helper.getView(R.id.webContent);
        TextView cornerMark = helper.getView(R.id.cornerMark);


        more.setOnClickListener(v -> {
            if (onMoreClickListener != null) {
                onMoreClickListener.onClick(more, curPos);
            }
        });
        if (webInfoEntity.lineMode == 1) {
            parentLayout.setAlpha(0.7f);
            offline.setVisibility(View.VISIBLE);
            webContent.setText("待联网验证");
        } else {
            parentLayout.setAlpha(1.0f);
            offline.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(fixedPushId) && fixedPushId.equals(webInfoEntity.webId)) {
                webContent.setText("");
                if (!TextUtils.isEmpty(pushPos) && Integer.parseInt(pushPos) > 0) {
                    cornerMark.setVisibility(View.VISIBLE);
                    cornerMark.setText(pushPos);
                } else {
                    cornerMark.setVisibility(View.GONE);
                }
            } else {
                cornerMark.setVisibility(View.GONE);
                webContent.setText(TextUtils.isEmpty(webInfoEntity.description) ? "暂无说明" : webInfoEntity.description);
            }
        }

        webTitle.setText(webInfoEntity.webName);
        Glide.with(activity).load(webInfoEntity.icon)
                .placeholder(R.drawable.default_logo)
                .error(R.drawable.default_logo)
                .centerInside()
                .dontAnimate()
                .into(logo);
        boolean isOnTop = webInfoEntity.onTop == 1 || webInfoEntity.serverTop == 1;
        title.setVisibility(isOnTop ? View.VISIBLE : View.INVISIBLE);
    }

    public interface OnMoreClickListener {
        void onClick(View view, int position);
    }

    public void setOnMoreClickListener(OnMoreClickListener onMoreClickListener) {
        this.onMoreClickListener = onMoreClickListener;
    }

    public void setFixedPushPos(String fixedPushId, String pushPos) {
        this.fixedPushId = fixedPushId;
        this.pushPos = pushPos;
    }
}
